package com.ytzl.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ytzl.common.CodeResultEnums;
import com.ytzl.common.ResponseData;
import com.ytzl.common.ResponseDataUtils;
import com.ytzl.vo.ItripUserVo;
import com.ytzl.service.ItripUserService;
import com.ytzl.model.ItripUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;



@Api(tags = "UserController")
@RestController
@RequestMapping("/api")
@Slf4j
public class ItripUserController {

    @Autowired
    private ItripUserService itripUserService;

    @GetMapping("/getAll")
    public ResponseData<ItripUser> getAll(){
        return ResponseDataUtils.buildSuccess(itripUserService.list());
    }

//    @PostMapping("/dologin")
//    public ResponseData<ItripUser> doLogin(@RequestBody ItripUserVo itripUserVo){
//        QueryWrapper<ItripUser> wrapper=new QueryWrapper<>();
//        wrapper.eq("usercode",itripUserVo.getUsercode());
//        ItripUser itripUser = itripUserService.getOne(wrapper);
//        if(itripUser==null){
//            return ResponseDataUtils.buildError(CodeResultEnums.USER_CODE_NOTEXIST);
//        }
//        HashMap<String,String> map=new HashMap<>();
//        map.put("usercode",itripUserVo.getUsercode());
//        String md5=SecureUtil.md5(itripUserVo.getUserpassword());
//        map.put("userpassword",md5);
//        wrapper.clear();
//        wrapper.allEq(map);
//        ItripUser user = itripUserService.getOne(wrapper);
//        if (user==null){
//            return ResponseDataUtils.buildError(CodeResultEnums.USER_CODE_ERRORPASSWORD);
//        }
//        return  ResponseDataUtils.buildSuccess(user);
//    }

    @ApiOperation("注册新用户")
    @PostMapping("/registerByName")
    public ResponseData<ItripUser> regNewUser(@RequestBody ItripUserVo itripUserVo){
            //throws ItripUserCodeNotRegistException{
        //1.检查用户是否已注册
        QueryWrapper<ItripUser> wrapper=new QueryWrapper<>();
        wrapper.eq("userCode",itripUserVo.getUsercode());
        ItripUser itripUser = itripUserService.getOne(wrapper);
        if (itripUser!=null){
            log.info("该用户已注册，请输入一个新的帐号名称。");
            //throw new ItripUserCodeNotRegistException();
            return ResponseDataUtils.buildError(CodeResultEnums.USER_CODE_EXIST);
        }
        //2.使用表单用户的信息组合数据
        ItripUser userModel=new ItripUser();
        userModel.setUsercode(itripUserVo.getUsercode());
        userModel.setUsername(itripUserVo.getUsername());
        //密码需要MD5加密
        String md5=SecureUtil.md5(itripUserVo.getUserpassword());
        log.info("{}",md5);
        userModel.setUserpassword(md5);
        userModel.setUsertype(1);
        userModel.setActivated(1);
        userModel.setCreationdate(new Date());
        userModel.setModifydate(new Date());

        boolean save = itripUserService.save(userModel);
        return ResponseDataUtils.buildSuccess(CodeResultEnums.SUCCESS);
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateById")
    public ResponseData<ItripUser> regUpdateUser(@RequestBody ItripUserVo itripUserVo){
        String md5=SecureUtil.md5(itripUserVo.getUserpassword());
        itripUserVo.setUserpassword(md5);
        boolean TOF = itripUserService.updateById(itripUserVo);
        if (TOF){
            return ResponseDataUtils.buildSuccess(CodeResultEnums.SUCCESS);
        }else{
            return ResponseDataUtils.buildError(CodeResultEnums.ERROR);
        }
    }

}